package com.example;

import com.example.reptile.Reptile;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * @Classname Test
 * @Description TODO
 * @Date 2022/4/17 16:33
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class Test {

    public static void main(String[] args) {
        test1();
    }

    public static void Test3(){

        Reptile reptile = new Reptile();
        WebDriver webDriver=reptile.getChromeDriver("D:\\chrome\\chromedriver.exe");
        webDriver.get("地址");
        try {
            Thread.sleep(5000);
        }catch (Exception e){
            e.printStackTrace();
        }
        List<WebElement> elementNodes = webDriver.findElements(By.xpath("//tr"));
        for (WebElement element : elementNodes) {
            System.out.println(element.getText()+" "+element.getAttribute("href"));
        }
        System.out.println("---------------------->");
        List<WebElement> elementNodes1 = webDriver.findElements(By.xpath("//*[@id=\"pl_top_realtimehot\"]/table/tbody/tr"));
        for (WebElement element : elementNodes1) {
            System.out.println(element.getText()+" "+element.getAttribute("href"));
        }
        System.out.println("===============================>");
        List<WebElement> elementNodes2 = webDriver.findElements(By.xpath("//table/tbody/tr"));
        for (WebElement element : elementNodes2) {
            // System.out.println(element.getText()+" "+element.getAttribute("href"));
            List<WebElement> tds=element.findElements(By.xpath("./td"));
            for (WebElement td : tds) {
                List<WebElement> as=td.findElements(By.xpath("./a"));
                for (WebElement a : as) {
                    System.out.println(a.getText()+" "+a.getAttribute("href"));
                }
                List<WebElement> spans=td.findElements(By.xpath("./span"));
                for (WebElement span : spans) {
                    System.out.println("访问人数："+span.getText());
                }
            }
        }
        System.out.println("===___====++++++++++++++++++++++++++++++++++");
    }

    public static void test2(){

        Reptile reptile = new Reptile();
        WebDriver webDriver=reptile.getChromeDriver("D:\\chrome\\chromedriver.exe");
        webDriver.get("地址");

        //按钮点击
        WebElement elementClick = webDriver.findElement(By.className("news-stream-basic-more"));
        elementClick.click();

        List<WebElement> elementNodes = webDriver.findElements(By.xpath("li[@class=\"news-stream-newsStream-news-item-has-image\"]"));

        for (WebElement element : elementNodes) {
            System.out.println(element.getText());
            List<WebElement> liList=element.findElements(By.xpath("./a"));
            for (WebElement elementWebs : liList) {
                System.out.println("----->");
                System.out.println(elementWebs.getText()+" "+element.getAttribute("href"));
            }
        }
    }


    public static void test1(){
        Reptile reptile = new Reptile();
        WebDriver webDriver=reptile.getChromeDriver("D:\\chrome\\chromedriver.exe");
        webDriver.get("地址");

        //按钮点击
        WebElement elementClick = webDriver.findElement(By.className("Common_1-1-344_3lDRV2"));
        elementClick.click();

        //获取的内容来自，经过谷歌访问处理后的element

        //选取div class名为index_1-1-344_2IAAkE 下的div 标签下 的 a标签
        List<WebElement> elementNodes = webDriver.findElements(By.xpath("*//div[@class=\"index_1-1-344_2IAAkE\"]/div/a"));
        for (int i = 0; i < elementNodes.size() ; i++) {
            WebElement webElement=elementNodes.get(i);
            //选取父节点下 div元素 class 为 index_1-1-344_3bRacm 的元素
            List<WebElement> aLists=webElement.findElements(By.xpath("./div[@class=\"index_1-1-344_3bRacm\"]"));
            for (int j = 0; j < aLists.size(); j++) {
                WebElement div = aLists.get(j);
                //获取父节点下 的div 元素
                List<WebElement> divLists= div.findElements(By.xpath("./div"));
                System.out.println("==================>");
                for (int o = 0; o < divLists.size(); o++) {
                    WebElement divOne = divLists.get(o);
                    if(divLists.size()==3){
                        if(o==0){
                            System.out.println("地区: "+divLists.get(0).getText().replaceAll("\n","-"));
                        }
                        if(o==1){
                            System.out.println("新增确诊: "+divLists.get(1).getText());
                        }
                        if(o==2){
                            System.out.println("新增无症状："+divLists.get(2).getText());
                        }
                    }
                    if(divLists.size() == 4){

                        if(o==1){
                            System.out.println("地区: "+divLists.get(0).getText()+":"+divLists.get(1).getText());
                        }
                        if(o==2){
                            System.out.println("新增确诊: "+divLists.get(2).getText());
                        }
                        if(o==3){
                            System.out.println("新增无症状："+divLists.get(3).getText());
                        }
                    }

                }
            }
        }
        //关闭web浏览器
      //  webDriver.close();
    }
}
